package com.erp.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

    private static DateFormat[] dateFormatList = {
            new SimpleDateFormat("yyyyMMddHHmmssS"),
            new SimpleDateFormat("yyyyMMddHHmmss"),
            new SimpleDateFormat("yyyyMMddHHmm"),
            new SimpleDateFormat("yyyyMMddHH"),
            new SimpleDateFormat("yyyyMMdd"),
            new SimpleDateFormat("yyyyMM")
    };

    public static Date parse(String source) {
        source = source.replace("-", "")
                .replace("/", "")
                .replace(":", "")
                .replace(" ", "");
        boolean flag = false;
        Date date = null;
        for (DateFormat dateFormat : dateFormatList) {
            try {
                date = dateFormat.parse(source);
                flag = true;
                break;
            } catch (ParseException e) {
            }
        }
        if (flag) {
            return date;
        } else {
            return null;
        }
    }
}
